/**
主要回顾：HTTP 响应

HTTP/1.1 200 OK                --响应行
Server: Apache-Coyote/1.1         --响应头（key-vaule）
Content-Length: 24 
Date: Fri, 30 Jan 2015 01:54:57 GMT
                                   --一个空行
this is hello servlet!!!                  --实体内容

常见状态码：
	100～199：成功接收请求，需要客户端继续提交下一次请求才能完成整个过程
	200～299：成功接收请求，并完成处理（一般用200）
	300～399：客户需要时一步细化请求（302，307，304）
	400～499：客户端请求有错（404）
	500～599：服务器端出现错误（500）

请求重定向：
	相当于超链接跳转页面。

 */

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Response
 */
@WebServlet("/Response")
public class Response extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Response() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//响应行
//		 response.setStatus(404);
//		 response.sendError(404);
		//响应头
//		response.setHeader("server", "JBoss");
		//实体内容
		
//		response.getWriter().write("hello world..." + new Date());
//		response.getOutputStream().write(10000);
		
		//请求重定向
//		response.setStatus(302);
//		response.setHeader("location", "/day09/testMethod.html");
		
		//定时刷新
//		response.setHeader("refresh", "1");
		
		//3秒后跳转
//		response.setHeader("refresh", "3;url=/day09/index.html");
		
		//content-Type
//		response.setContentType("text/html;charset=utf-8");
//		response.setContentType("text/xml;charset=utf-8");
//		response.getWriter().write("<html><head><title>Title</title></head><body>this is body</body></html>");
		
//		File file = new File("/Users/Chuck/Love/Java/Entry/day09/WebContent/1.jpg");
//		response.setContentType("image/jpg");	//直接打开图片
//		//以下这个就会提示用户下载图片
//		response.setHeader("Content-Disposition", "attachment; filename=" + file.getName());
//		
//		FileInputStream in = new FileInputStream(file);
//		byte[] buf = new byte[1024];
//		int len = -1;
//		while ((len = in.read(buf)) != -1) {
//			response.getOutputStream().write(buf, 0, len);
//		}
//		in.close();
		
		//编码
		response.setCharacterEncoding("utf-8");
		response.getWriter().write("中文");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	
}
